package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.api.model.vo.AgentInfoVo;
import com.weixing.mall.provider.mapper.AgentInfoMapper;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentInfo;
import com.weixing.mall.provider.model.domain.AgentLog;
import com.weixing.mall.provider.model.dto.AgentInfoEditDto;
import com.weixing.mall.provider.model.enums.InfoPerfectEnum;
import com.weixing.mall.provider.service.IAgentInfoService;
import com.weixing.mall.provider.service.IAgentLogService;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.provider.util.InfoPerfectUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 代理商模块-代理商基础信息表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentInfoServiceImpl extends ServiceImpl<AgentInfoMapper, AgentInfo> implements IAgentInfoService {
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentLogService agentLogService;

    @Override
    public AgentInfoVo selectByAgentId(Long agentId) {
        AgentInfoVo agentInfoVo = baseMapper.selectByAgentId(agentId);
        if (null == agentInfoVo) {
            agentInfoVo = new AgentInfoVo();
        }
        return agentInfoVo;
    }

    @Override
    public Long saveInfo(AgentInfoEditDto dto, Long uid) {

        Long agentId = dto.getAgentId();
        AgentInfo info = null;
        if (null == dto.getId()) {
            info = new AgentInfo();
            BeanUtils.copyProperties(dto, info);
            save(info);
        } else {
            info = getById(dto.getId());
            BeanUtils.copyProperties(dto, info);
            updateById(info);
        }
        Agent agent = agentService.getById(agentId);
        agent.setIsInfoPerfect(InfoPerfectUtil.add(agent.getIsInfoPerfect(), InfoPerfectEnum.INFO));
        agentService.updateById(agent);

//        AgentLog agentLog = agentLogService.getOne(new QueryWrapper<AgentLog>().eq("agent_id", agentId));
//        agentLog.setIdCard(dto.getIdCard());
//        agentLogService.updateById(agentLog);
        return info.getId();
    }
}
